package com.rttmall.shopbackend.app.gambling.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rttmall.shopbackend.app.gambling.mapper.GamblingRecordAddressMapper;
import com.rttmall.shopbackend.app.gambling.mapper.GamblingRecordCustomMapper;
import com.rttmall.shopbackend.app.gambling.pojo.Express;
import com.rttmall.shopbackend.app.gambling.pojo.GamblingRecordAddress;
import com.rttmall.shopbackend.app.gambling.pojo.GamblingRecordCustom;
import com.rttmall.shopbackend.app.gambling.service.GamblingRecordService;
import com.rttmall.shopbackend.enums.GamblingRecordAddressStatus;
import com.rttmall.shopbackend.enums.GamblingRecordAddressZiti;

@Service
public class GamblingRecordServiceImpl implements GamblingRecordService {
	@Autowired
	private GamblingRecordCustomMapper gamblingRecordCustomMapper;
	
	@Autowired
	private GamblingRecordAddressMapper gamblingRecordAddressMapper;
	
	@Override
	public Map queryObjectPrizeShow(GamblingRecordCustom gamblingRecordCustom) {
		Map map = new HashMap();
		List<GamblingRecordCustom> orderCustomList = gamblingRecordCustomMapper
				.queryObjectPrizeShow(gamblingRecordCustom);
		orderCustomList.forEach(w->{
        	String endString = "";
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	if(w.getBuyTime()!=null){
            	Date endDate = new Date(w.getBuyTime());
            	endString = sdf.format(endDate);
        	}
    		w.setBuyTimeDate(endString);
    		String completDate = "";
        	if(w.getCompleteTime()!=null){
            	Date endDate = new Date(w.getCompleteTime());
            	completDate = sdf.format(endDate);
        	}
    		w.setCompleteTimeDate(completDate);
    		if(w.getGamblingRecordAddress() == null){
    			
    		}else{
    			if(w.getGamblingRecordAddress().getZiti()){
        			w.setGamblingRecordAddressZitiEnum(GamblingRecordAddressZiti.GET);
        		}else if(!w.getGamblingRecordAddress().getZiti()){
        			w.setGamblingRecordAddressZitiEnum(GamblingRecordAddressZiti.POST);
        		}
    			if(w.getGamblingRecordAddress().getIsSend()){
        			w.setGamblingRecordAddressEnum(GamblingRecordAddressStatus.SEND);
        		}
    		}
    		
    		
        });
		int sum = gamblingRecordCustomMapper.queryObjectPrizeShowCount(gamblingRecordCustom);
		 map.put("data", orderCustomList);
	     map.put("total", sum);
	     return map;
	}

	@Override
	public Map queryVirtualPrizeShow(GamblingRecordCustom gamblingRecordCustom) {
		Map map = new HashMap();
		List<GamblingRecordCustom> orderCustomList = gamblingRecordCustomMapper
				.queryVirtualPrizeShow(gamblingRecordCustom);
		orderCustomList.forEach(w->{
        	String endString = "";
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	if(w.getBuyTime()!=null){
            	Date endDate = new Date(w.getBuyTime());
            	endString = sdf.format(endDate);
        	}
    		w.setBuyTimeDate(endString);
    		String completDate = "";
        	if(w.getCompleteTime()!=null){
            	Date endDate = new Date(w.getCompleteTime());
            	completDate = sdf.format(endDate);
        	}
    		w.setCompleteTimeDate(completDate);
        });
		int sum = gamblingRecordCustomMapper.queryVirtualPrizeShowCount(gamblingRecordCustom);
		 map.put("data", orderCustomList);
	     map.put("total", sum);
	     return map;
	}

	@Override
	public GamblingRecordCustom editObjectPrize(int parseInt) {
		GamblingRecordCustom recordCustom = gamblingRecordCustomMapper.queryObjectPrize(parseInt);
		String endString = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	if(recordCustom.getBuyTime()!=null){
        	Date endDate = new Date(recordCustom.getBuyTime());
        	endString = sdf.format(endDate);
    	}
    	recordCustom.setBuyTimeDate(endString);
		String completDate = "";
    	if(recordCustom.getCompleteTime()!=null){
        	Date endDate = new Date(recordCustom.getCompleteTime());
        	completDate = sdf.format(endDate);
    	}
    	recordCustom.setCompleteTimeDate(completDate);
		return recordCustom;
	}
	
	@Override
	public GamblingRecordCustom editVirtualPrize(int parseInt) {
		GamblingRecordCustom recordCustom = gamblingRecordCustomMapper.queryVirtualPrize(parseInt);
		String endString = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	if(recordCustom.getBuyTime()!=null){
        	Date endDate = new Date(recordCustom.getBuyTime());
        	endString = sdf.format(endDate);
    	}
    	recordCustom.setBuyTimeDate(endString);
		String completDate = "";
    	if(recordCustom.getCompleteTime()!=null){
        	Date endDate = new Date(recordCustom.getCompleteTime());
        	completDate = sdf.format(endDate);
    	}
    	recordCustom.setCompleteTimeDate(completDate);
		return recordCustom;
	}

	@Override
	public Map queryAllExpress() {
		Map map = new HashMap();
		List<Express> list= gamblingRecordCustomMapper.queryAllExpress();
		map.put("data", list);
		return map;
	}

	@Override
	public void updateRecordAddressSubmit(GamblingRecordCustom gamblingRecordCustom) {
		GamblingRecordAddress gamblingRecordAddress = gamblingRecordCustom.getGamblingRecordAddress();
		gamblingRecordAddress.setRecordId(gamblingRecordCustom.getId());
		if(gamblingRecordAddress.getId() == null){
			gamblingRecordAddressMapper.insertSelective(gamblingRecordAddress);
		}else{
			gamblingRecordAddressMapper.updateByPrimaryKeySelective(gamblingRecordAddress);
		}
	}
	

	@Override
	public Map queryGamblingPeriodCustomer(
			GamblingRecordCustom gamblingRecordCustom) {
		Map map = new HashMap();
        List<GamblingRecordCustom> gamblingRecordCustomList = gamblingRecordCustomMapper.queryGamblingRecord(gamblingRecordCustom);
        gamblingRecordCustomList.forEach(w->{
        	String endString = "";
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	Date createDate = new Date(w.getCreateTime());
        	if(w.getCreateTime()!=null){
            	Date endDate = new Date(w.getCreateTime());
            	endString = sdf.format(endDate);
        	}
    		w.setBuyTimeDate(endString);
        });
        int sum = gamblingRecordCustomMapper.queryGamblingRecordCount(gamblingRecordCustom);
        map.put("data", gamblingRecordCustomList);
        map.put("total", sum);
        return map;
	}
}
